// 根据tsconfig.json配置执行ts转换
import typescript from 'rollup-plugin-typescript2'
// 帮助 Rollup 查找外部模块
import resolve from '@rollup/plugin-node-resolve'
// 将CommonJS模块转换为 ES2015 供 Rollup 处理
import commonjs from '@rollup/plugin-commonjs'
// Rollup 集成 Babel，转义代码
import babel from '@rollup/plugin-babel'
// 查看打包文件大小
import filesize from 'rollup-plugin-filesize'
// 压缩代码
import terser from '@rollup/plugin-terser'
// 样式
import postcss from 'rollup-plugin-postcss'
// 自动添加平台适配前缀
import autoprefixer from 'autoprefixer'
// 忽略打包
import peerDepsExternal from  'rollup-plugin-peer-deps-external'

const packageJson = require('./package.json')


export default {
  input: 'src/index.ts',
  output: [
    // {
    //   file: packageJson.main,
    //   format: 'cjs',
    //   sourcemap: true
    // },
    // {
    //   file: packageJson.module,
    //   format: 'esm',
    //   sourcemap: true
    // }
    {
      file: packageJson.main,
      format: 'esm',
      sourcemap: true
    }
  ],
  plugins: [
    resolve(
      {
        alias: {
          '@': 'src'
        }
      }
    ),
    commonjs(),
    postcss({
      plugins: [
        autoprefixer()
      ],
      extensions: ['.css', '.scss'], // 使用SCSS
      extract: true, // 提取到单独的文件
      minimize: true, // 压缩CSS
      sourceMap: true, // 生成sourceMap
    }),
    typescript({ tsconfig: 'tsconfig.json' }),
    babel({
      presets: [
        '@babel/preset-env',
        '@babel/preset-typescript',
        [
          '@babel/preset-react',
          {
            runtime: 'automatic'
          }
        ]
      ],
      babelHelpers: 'bundled',
      extensions: ['.js', '.jsx', '.ts', '.tsx'],
      exclude: '**/node_modules/**'
    }),
    terser(),
    filesize(),
    peerDepsExternal()
  ],
  // external: ['react', 'react-dom', '@tarojs/components', '@tarojs/react', '@tarojs/taro']
}
